<?php
namespace Index\Controller;
use Think\Controller;
use Think\Upload;
use Vendor\ThinkImage\ThinkImage;
use Index\Controller\CommonController;

class CustomerController extends CommonController {
    public function indexAction(){
    
		$id = $_SESSION['uid'];

		$Model = M();
        $sql = "select truename,sex, province,address,username,face from user where id = '{$id}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);
		$this->display('index');
    }

    //保存个人资料的ajax
    public function accountAction(){
    	if(!IS_AJAX){
    		$data = "非法的请求方式";
           $this->ajaxReturn($data);
        }

    	$post = I('post.');
    	$id = $_SESSION['uid'];
    	$Model = M();
		$sql = "update user set truename = '{$post['realName']}',sex = '{$post['sex']}',province = '{$post['province']}',address = '{$post['address']}' where id = '{$id}'";
		if($Model->execute($sql)){
			$data = '保存成功';	
		}else{
			$data = '数据已保存，如有更新请填写完整';	
		}
		$this->ajaxReturn($data);

    }

 	//密码
    public function passwordAction(){
    	$id = $_SESSION['uid'];
		$Model = M();
        $sql = "select * from user where id = '{$id}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);

        $this->display();	
    }

    //修改密码
    public function pwdmodfiyAction(){
    	if(!IS_AJAX){
    		$data = "非法的请求方式";
           $this->ajaxReturn($data);
        }

    	$id = $_SESSION['uid'];
    	$postData = I('post.');

        if(empty($postData['oldPwd']) || empty($postData['newPwd']) || empty($postData['reNewPwd']) ){
            $data = "密码不能留空";
			$this->ajaxReturn($data);
        }

        if(strlen($postData['newPwd'])<6){
            $data = "新密码少于6位";
			$this->ajaxReturn($data);
        }


        if($postData['newPwd'] != $postData['reNewPwd']){
            $data = "新密码不一致";
			$this->ajaxReturn($data);
        }

        $Model = M();
        $sql = "select password from user where id = '{$id}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $newPostdata = md5($postData['newPwd']);
        $oldPostdata = md5($postData['oldPwd']);
        if($oldPostdata != $userInfo['password']){
            $data = "你输入的旧密码不正确";
			$this->ajaxReturn($data);
        }else if($newPostdata == $userInfo['password']){
            $data = "新密码不能与旧密码相同";
			$this->ajaxReturn($data);
        }else{
            $sql = "update user set password = '{$newPostdata}' where id = '{$id}'";
           	$Model->execute($sql);
            $data = "保存成功";
			$this->ajaxReturn($data);   
        } 
    }


 	//头像
    public function faceAction(){
    	$id = $_SESSION['uid'];
		$Model = M();
        $sql = "select username,face from user where id = '{$id}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);
        
 		$this->display('face');
    }

   



	//上传头像
	public function uploadImgAction(){

		$upload = new Upload(C('UPLOAD_CONFIG'));	// 实例化上传类
		//头像目录地址
		$path = './Avatar/';
		if(!$info=$upload->upload()) {						
		// 上传错误提示错误信息
			$this->ajaxReturn(array('status'=>0,'info'=>$upload->getError()));
		}else{	
			// 上传成功 获取上传文件信息	
			$url = $info['Filedata']['savename'];
			$temp_size = getimagesize($path.$url);
			if($temp_size[0] < 100 || $temp_size[1] < 100){
			//判断宽和高是否符合头像要求
				$this->ajaxReturn(array('status'=>0,'info'=>'图片宽或高不得小于100px！'));
			}
			$this->ajaxReturn(array('status'=>1,'path'=>__ROOT__.'/Avatar/'.$url));
		}
	}

	//裁剪并保存用户头像
	public function cropImgAction(){
		//图片裁剪数据
		$params = I('post.');
		
	
		//裁剪参数
		if(!isset($params) && empty($params)){
			$this->error('参数错误！');
		}

		//头像目录地址
		$path = './Avatar/';

		//要保存的图片
		$real_path = $path.uniqid().'.jpg';
		//临时图片地址
		$src = $params['src'];
		$srcArr = explode('/',$src);
		$pic_path = $path.end($srcArr);
	
		//实例化裁剪类
		$Think_img = new ThinkImage(THINKIMAGE_GD);
		//裁剪原图得到选中区域
		$Think_img->open($pic_path)->crop($params['w'],$params['h'],$params['x'],$params['y'])->save($real_path);

		@unlink($pic_path);
		$pic_path = substr($real_path, 1);
		$id = $_SESSION['uid'];
		$Model = M();
		$sql = "update user set face = '{$pic_path}' where id = '{$id}'";
		$Model -> execute($sql);
		
	
		//生成缩略图
		// $Think_img->open($real_path)->thumb(100,100, 1)->save($path.'avatar_100.jpg');
		// $Think_img->open($real_path)->thumb(60,60, 1)->save($path.'avatar_60.jpg');
		// $Think_img->open($real_path)->thumb(30,30, 1)->save($path.'avatar_30.jpg');
		$this->success('上传头像成功');
	}


}